package com.gdth.http.method;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class httpsUtils {
	
	 public static SSLSocketFactory getSSLSocketFactory() {
	        try {
	            SSLContext sslContext = SSLContext.getInstance("SSL");
	            sslContext.init(null, getTrustManager(), new SecureRandom());
	            return sslContext.getSocketFactory();
	        } catch (Exception e) {
	            throw new RuntimeException(e);
	        }
	    }

	   
	    //获取TrustManager
	    private static TrustManager[] getTrustManager() {
	        //不校检证书链
	       final TrustManager[] trustAllCerts = new TrustManager[]{
	                new X509TrustManager() {

						@Override
						public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
								throws CertificateException {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
								throws CertificateException {
							// TODO Auto-generated method stub
							
						}

						@Override
						public java.security.cert.X509Certificate[] getAcceptedIssuers() {
							// TODO Auto-generated method stub
							
							return   new X509Certificate[]{};
						}
	                }
	        };
	     return trustAllCerts;
	       
	    }


	  //获取HostnameVerifier 
	    public static HostnameVerifier getHostnameVerifier() { 
	      HostnameVerifier hostnameVerifier = new HostnameVerifier() { 
	        @Override
	        public boolean verify(String s, SSLSession sslSession) { 
	          return true; 
	        } 
	      }; 
	      return hostnameVerifier; 
	    }
}
